#include "StdAfx.h"
#include "RenderCameraController.h"


RenderCameraController::RenderCameraController( Ogre::Camera * cam, Ogre::Real look_speed, Ogre::Real travel_speed ): mCamera( cam ), mLookSpeed(look_speed), mTravelSpeed(travel_speed){}
RenderCameraController::~RenderCameraController(){}

bool RenderCameraController::isControlDown() const{ return mCtrl; }
bool RenderCameraController::isAltDown()const{ return mAlt; }
bool RenderCameraController::isShiftDown() const{ return mShift; }

Ogre::Real RenderCameraController::lookSpeed() const{ return mLookSpeed; }
Ogre::Real RenderCameraController::travelSpeed() const{ return mTravelSpeed; }

Ogre::Camera* RenderCameraController::camera(){ return *mCamera; }
const Ogre::Camera* RenderCameraController::camera() const{ return *mCamera; }
	
void RenderCameraController::setLookSpeed( Ogre::Real spd ){ mLookSpeed = spd; }
void RenderCameraController::setTravelSpeed( Ogre::Real spd ){ mTravelSpeed = spd; }

void RenderCameraController::pan( Ogre::Real angle_x, Ogre::Real angle_y )
{
	Ogre::Vector3 d( right()*angle_x );
	d.y = -angle_y;
	//Ogre::LogManager::getSingleton().logMessage(Lit::String("Panning (")(angle_x)(", ")(angle_y)(") -> ")(d).str() );
	move( d );
}
void RenderCameraController::tilt( Ogre::Real angle_x, Ogre::Real angle_y )
{
	mCamera->pitch( Ogre::Degree(-angle_y) ); //0.07
	mCamera->yaw( Ogre::Degree(-angle_x) );
}
Ogre::Vector3 RenderCameraController::right() const{ return mCamera->getRight(); }
Ogre::Vector3 RenderCameraController::direction() const{ return mCamera->getDirection(); }
Ogre::Vector3 RenderCameraController::up() const{ return mCamera->getUp(); }

void RenderCameraController::checkModifiers( )
{
	mAlt = ( GetAsyncKeyState(VK_LMENU) & 0x8000 ) || ( GetAsyncKeyState(VK_RMENU) & 0x8000 );
	mShift = ( GetAsyncKeyState(VK_LSHIFT) & 0x8000 ) || ( GetAsyncKeyState(VK_RSHIFT) & 0x8000 );
	mCtrl = ( GetAsyncKeyState(VK_CONTROL) & 0x8000 );
}
void RenderCameraController::updateMouse( Ogre::Real mouse_dx, Ogre::Real mouse_dy, Ogre::Real frame_dt )
{
	if( mCtrl )
	{
		pan( mouse_dx * frame_dt * (mLookSpeed*(mShift?3.0f:1.0f)), mouse_dy * frame_dt * (mLookSpeed*(mShift?3.0f:(mAlt?0.3f:1.0f))) );
	}
	else
	{
		tilt( mouse_dx * frame_dt * mLookSpeed, mouse_dy * frame_dt * mLookSpeed );
	}
}
void RenderCameraController::updateKeyboard(Ogre::Real frame_dt)
{
	checkModifiers( );
	if(( GetAsyncKeyState( 'Z' ) & 0x8000 )|| ( GetAsyncKeyState( 'W' ) & 0x8000 )) move( direction() * frame_dt * (mTravelSpeed*(mShift?3.0f:(mAlt?0.3f:1.0f))) );
	if( GetAsyncKeyState( 'S' ) & 0x8000 ) move( -direction() * frame_dt * (mTravelSpeed*(mShift?3.0f:(mAlt?0.3f:1.0f))) );
	if(( GetAsyncKeyState( 'Q' ) & 0x8000 ) || ( GetAsyncKeyState( 'A' ) & 0x8000 )) move( -right() * frame_dt * (mTravelSpeed*(mShift?3.0f:(mAlt?0.3f:1.0f))) );
	if( GetAsyncKeyState( 'D' ) & 0x8000 ) move( right() * frame_dt * (mTravelSpeed*(mShift?3.0f:(mAlt?0.3f:1.0f))) );
}
void RenderCameraController::move( Ogre::Real x, Ogre::Real y, Ogre::Real z ){ mCamera->move( Ogre::Vector3( x, y, z ) ); }
void RenderCameraController::move( const Ogre::Vector3 &dir ){ mCamera->move( dir ); }